package charles.testproject.guithreads;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class GUIThreads {
	
	JFrame jf = new JFrame("GUIThreads");
	Button bA = new Button("Button A");
	Button bB = new Button("Button B");
	Button bC = new Button("Button C");
	JTextField jD = new JTextField(20);
	FlowLayout fL = new FlowLayout();
	BorderLayout bL = new BorderLayout();
	
	public GUIThreads() {
		bA.addActionListener(new ActionLitenerLeftLatout());
		bB.addActionListener(new ActionLitenerLeftLatout());
		bC.addActionListener(new ActionLitenerLeftLatout());
//		jf.setLayout(fL);
		jf.setLayout(bL);
		jf.add(bA,BorderLayout.EAST);
		jf.add(bB,BorderLayout.WEST);
		jf.add(bC,BorderLayout.NORTH);
		jf.add(jD,BorderLayout.SOUTH);
		jf.setSize(320,200);
		jf.setVisible(true);
	}
	
	public static void main(String[] args) {
		GUIThreads g = new GUIThreads();
//		MyThread1 mythread1 = new MyThread1();
//		Thread t1 = new Thread(mythread1);
//		t1.start();
		
		//打印出当前的线程
		int NumberOfThread = Thread.activeCount();
		Thread[] tg = new Thread[NumberOfThread];
		Thread.currentThread().enumerate(tg);
		for(int i = 0 ; i < tg.length; i++) {
			System.out.println(tg[i].getName());
		}
	}

	class ActionLitenerLeftLatout implements ActionListener{

		public void actionPerformed(ActionEvent event) {
			if(event.getSource() == bA) {
				fL.setAlignment(FlowLayout.LEFT);
				System.out.println("come on A");
			}
			if(event.getSource() == bB) {
				fL.setAlignment(FlowLayout.RIGHT);
				System.out.println("come on B");
			}
			if(event.getSource() == bC) {
				fL.setAlignment(FlowLayout.CENTER);
				System.out.println("come on C");
			}
			fL.layoutContainer(jf);
		}
	}
	
}

//class MyThread1 implements Runnable {
//	int i = 0;
//	public void run() {
//		while(true) {
//		try {
//				Thread.sleep(2000);
//				System.out.println("myThread1 start!");
//				i++;
//			} catch (InterruptedException e) {
//				e.printStackTrace();
//			}
//			if(i >4) break;
//		}
//	}
//}
